/**
 * @author 刘超
 * @Note 右下角弹出层[仿QQ消息弹出消息].
 * @UsePage: [Index]
 * @version 1.3
 * Demo:
 */
(function (){
	Popup = function (params){
		this.div = null;          // 窗体对象
		this.divId = "popupDiv";  // 窗体ID						支持参数赋值
		this.divWidth = 255;      // 窗体宽						支持参数赋值
		this.divHeight = 150;     // 窗体高						支持参数赋值

		this.divHeader = null;        // 窗体头 对象
		this.divHeaderHeight = 25;    // 窗体头 对象高度			支持参数赋值

		this.divTitle = "<b>Title</b>";  // 窗体标题				支持参数赋值

		this.xbtnPrefix = "XBTN"; // 关闭按钮的后缀				支持参数赋值

		this.divContent = null;       // 窗体内容对象
		this.divSound = null;         // 窗体声音层对象

		this.findInterval = 100;   // 寻找某个对象是否存在间隔时间				支持参数赋值
		this.showInterval = 600000; // 循环显示间隔时间 默认10分钟					支持参数赋值
		this.changeInterval = 10;  // 改变窗体间隔时间							支持参数赋值
		this.countdownTime = 60;   // 单位:秒(s) 窗体停留多少						支持参数赋值
		this.changeSpeed = 1;      // 窗体每次上升/下降多少像素					支持参数赋值

		this.countdownSTO     = null;  // 窗口关闭倒计时对象    sto:setTimeout
		this.isClosing        = false; // True为正在关闭.
		this.isOpenned        = false; // True为正在显示
		this.isOpenning       = false; // True为正在打开.
		this.isHover          = false; // True为鼠标悬停
//		this.isShowInterval   = false; // True为开启循环显示窗体 [窗体循环间隔有问题,留到2.0再解决]
		this.isStartCountdown = false; // True 倒计时关闭的时间到了

		this.screenPosition = {};

		this.init(params);
	}
	Popup.prototype.getScreenSize = function (){
		var this_ = this;
		function start(){
			if(document.body){
				var x = document.body.clientWidth;
				var y = document.body.clientHeight;

				this_.screenPosition.x = x - this_.divWidth;
//				this_.screenPosition.y = y - this_.divHeight;
				this_.screenPosition.y = y;
			}else{
				setTimeout(start, this_.findInterval);
			}
		}
		start();
	};
	Popup.prototype.init = function (params){

		this.loadParams(params);
		this.getScreenSize();
		this.createPopupDiv();
	};
	Popup.prototype.loadParams = function (params){
		if(params){
			for(var key in params){
				this[key] = params[key];
			}
		}
	};
	/**
	 * 显示窗体
	 */
	Popup.prototype.show = function (){
		var this_ = this;

		function start(){
			if(this_.div){

				if(this_.isOpenning || this_.isOpenned){
					return;
				}
				this_.isOpenning = true;
				this_.div.style.display = "";
				this_.isOpenned  = true;    // 从这里开始,窗口正在升起显示
				this_.changeDivSize(true);
			}else{
				setTimeout(start, this_.findInterval);
			}
		}
		start();
	};
	/**
	 * 关闭窗体
	 */
	Popup.prototype.close = function (){
		if(this.isClosing){
			return ;
		}
		this.isClosing = true;
		if(this.countdownSTO){
			clearTimeout(this.countdownSTO);
		}
		this.changeDivSize(false);
	};
	/**
	 * 间隔(多长时间)自动显示
	 */
	Popup.prototype.intervalDisplay = function (){
		var this_ = this;
		var s = this_.showInterval+this_.countdownTime*1000;
		function start(){
			if(!this_.isOpenned){
				this_.show();
			}
			setTimeout(start, s);
		}
		setTimeout(start, s);
	};
	/*****************设置样式*********************/
	Popup.prototype.setStyle = function (dom,styles){
		var this_ = this;
		function start(){
			if(this_[dom] && this_[dom].style){
				var divStyle = this_[dom].style;
				if(styles){
					for(var s in styles){
						divStyle[s] = styles[s];
					}
				}
			}else{
				setTimeout(start, this_.findInterval);
			}
		}
		start();
	};
	Popup.prototype.setDivStyle = function (styles){ // 设置div 样式 | 请不要设置宽高
		this.setStyle("div",styles);
	};
	Popup.prototype.setDivHeaderStyle = function (styles){ // 设置div header 样式
		this.setStyle("divHeader",styles);
	};
	Popup.prototype.setDivContentStyle = function (styles){ // 设置div content 样式
		this.setStyle("divContent",styles);
	};

	/*****************设置内容*****************/
	Popup.prototype.setContent = function (html){
		var this_ = this;
		function start(){
			if(this_.divContent){
				this_.divContent.innerHTML = html;
			}else{
				setTimeout(start, this_.findInterval);
			}
		}
		start();
	}
	Popup.prototype.setSound = function (html){
		var this_ = this;
		function start(){
			if(this_.divSound){
				this_.divSound.innerHTML = html;
			}else{
				setTimeout(start, this_.findInterval);
			}
		}
		start();
	}

	Popup.prototype.clearSound = function (){
		if(this.divSound){
			this.divSound.innerHTML = "";
		}
	}
	/**
	 * 初始化div
	 */
	Popup.prototype.createPopupDiv = function(){
		var this_ = this;
		function bindCloseFn(ppp){ // 这是为了保留当前数据才出入了当前的popup对象
			var xBtn = document.getElementById(ppp.divId+ppp.xbtnPrefix);
			xBtn.onclick = function (){
				ppp.div.style.display = "none";
				ppp.div.style.top = this_.screenPosition.y + "px";
				ppp.isStartCountdown = false;
				ppp.isClosing = false;
				ppp.isOpenned = false; // 窗体从现在开始,已经完全消失关闭.
			};
			xBtn.onmouseover = function (){
				this.style.cursor="pointer";
				this.className = "close_a";
			};
			xBtn.onmouseout = function (){
				this.className = "close_b";
			};
		}
		function start(){
			if(document.body){
				var div = document.createElement("div");
				div.id = this_.divId;
				div.style.width = (this_.divWidth + "px");
				div.style.height = (this_.divHeight + "px");
				div.style.top = this_.screenPosition.y + "px";
				div.style.left = this_.screenPosition.x + "px";
				div.style.position = "absolute";
//				div.style.border = "2px solid #B7E3FE";
//				div.style.background = "white";
				div.style.zIndex = "999";
				div.style.overflow = "hidden";
				div.style.display = "none";
				document.body.appendChild(div);

				this_.div = div;

				var divHeader = document.createElement("div");
				divHeader.style.width = "100%";
				divHeader.style.height = this_.divHeaderHeight + "px";
//				divHeader.style.borderBottom = "2px solid #B7E3FE";
//				divHeader.style.background = "#B7E3FE";
				div.appendChild(divHeader);

				this_.divHeader = divHeader;

				var tableStr = "";
				tableStr += "<b class='rtop'><b class='r1'></b><b class='r2'></b><b class='r3'></b><b class='r4'></b></b>";
				tableStr += "<table class='titleTab' style='width:100%;'>";
				tableStr += "<tr><td style='width:98%;'>";
				tableStr += "<div id='divTitle'>"+this_.divTitle+"</div>";
				tableStr += "</td><td>";
				tableStr += "<div id='"+(this_.divId+this_.xbtnPrefix)+"' class='close_b'>";
//				tableStr += "&nbsp;X&nbsp;";
				tableStr += "</div>";
				tableStr += "</td></tr>";
				tableStr += "</table>";

				divHeader.innerHTML = tableStr;
				bindCloseFn(this_); // 参数必不可少

				var divContent = document.createElement("div");
				divContent.style.width = this_.divWidth;
				divContent.style.height = ((this_.divHeight - this_.divHeaderHeight) + "px");
				divContent.style.border = "2px solid #B7E3FE";
				divContent.style.borderTop = "0";
				divContent.style.overflow = "auto";
				divContent.style.background = "white";
				div.appendChild(divContent);

				this_.divContent = divContent;

				var divSound = document.createElement("div");
				div.appendChild(divSound);

				this_.divSound = divSound;


				hover(this_.div);
			}else{
				setTimeout(start, this_.findInterval);
			}
		}
		function hover(dom){ // 悬停操作
			dom.onmouseover = function (){
				this_.isHover = true;
			};
			dom.onmouseout = function (){
				this_.isHover = false;
				if(this_.isStartCountdown){
					this_.close();
				}
			};
		}
		start();
	};
	/**
	 * 改变窗体大小
	 * true为变大
	 * false为变小
	 */
	Popup.prototype.changeDivSize = function (change){
		var this_ = this;
		var currHeight = (document.body.clientHeight - parseInt(this.div.style.top));
		var size = change?0:currHeight;
		function start(){
			size = (change ? size+this_.changeSpeed : size-this_.changeSpeed);

			if(this_.isHover){
				size = this_.divHeight+1;
				this_.isClosing = false;
			}

			this_.div.style.top = this_.screenPosition.y - size;

			if(size <0 || size>this_.divHeight){
				if(size < 0){
					this_.div.style.display = "none";
					this_.isStartCountdown = false;
					this_.isClosing = false;
					this_.isOpenned = false; // 窗体从现在开始,已经完全消失关闭.
					this_.clearSound();
				}
				if(size > this_.divHeight){
					this_.isOpenning = false;
					this_.countdown();
				}
				return ;
			}
			setTimeout(start,this_.changeInterval);
		}
		start();
	};
	/**
	 * 关闭窗体倒计时
	 */
	Popup.prototype.countdown = function (){
		var this_ = this;

		if(this_.isStartCountdown){
			return ;
		}

		var ct = this_.countdownTime;
		function start(){
			ct--;
			if(ct < 0){
				this_.isStartCountdown = true;
				if(this_.isHover){
					clearTimeout(this.countdownSTO);
				}else{
					this_.close();
				}

				return ;
			}
			this_.countdownSTO = setTimeout(start,1000);
		}
		start();
	};

//	var popup = new Popup({divHeight:400,divWidth:300});
//	var popup = new Popup({divTitle:"新事件列表"});
//	popup.loadParams({divHeight:100,divWidth:200});
//	popup.setDivStyle({border:""})
//	popup.setDivHeaderStyle({background:"url(/scripts/System/_resource/MzForm/corner.png)"});
//	popup.setDivContentStyle({border:"2px double gray"});
//	popup.show();
//	popup.setContent("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab<br><br><br><br><br>aaaaaaaaaaa<br><br><br><br>aaaaaaaaaaaaaa");
//	popup.setSound("声音代码"); // 不可用
//	popup.intervalDisplay();
//	var ppp = new Popup({divId:"abcdddd",showInterval:20000,countdownTime:5});
//	ppp.show();
//	ppp.intervalDisplay();
})();